package com.internetCafes.spms.web.toolbox.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.internetCafes.spms.common.utils.ShiroUtils;
import com.internetCafes.spms.core.exception.RRException;
import com.internetCafes.spms.web.toolbox.entity.WebsiteConfig;
import com.internetCafes.spms.web.toolbox.entity.dto.website.WebsiteEditDto;
import com.internetCafes.spms.web.toolbox.entity.dto.website.WebsiteListDto;
import com.internetCafes.spms.web.toolbox.entity.vo.website.WebsiteAllVo;
import com.internetCafes.spms.web.toolbox.entity.vo.website.WebsiteListVo;
import com.internetCafes.spms.web.toolbox.entity.vo.website.WebsiteTypesVo;
import com.internetCafes.spms.web.toolbox.mapper.WebsiteConfigMapper;
import com.internetCafes.spms.web.toolbox.service.IWebsiteConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.nlpcn.commons.lang.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 常用网站配置 服务实现类
 * </p>
 *
 * @author Zj
 * @since 2021-06-09
 */
@Service
public class WebsiteConfigServiceImpl extends ServiceImpl<WebsiteConfigMapper, WebsiteConfig> implements IWebsiteConfigService {


    /**
     * 获取所有网址，前端展示
     */
    @Override
    public List<WebsiteAllVo> all() {
        List<WebsiteConfig> websiteConfigsAll = baseMapper.selectList(new QueryWrapper<>());
        // 获取网址类别
        List<WebsiteAllVo> result = websiteConfigsAll.stream()
                .filter(websiteConfig -> websiteConfig.getType().equals(1) && websiteConfig.getStatusFlag().equals(0))
                .sorted(Comparator.comparing(WebsiteConfig::getOrderNum))
                .map(websiteConfig -> {
                    WebsiteAllVo websiteAllVo = new WebsiteAllVo();

                    List<WebsiteAllVo> children = websiteConfigsAll.stream()
                            .filter(websiteInfo -> websiteConfig.getCode().equals(websiteInfo.getTypeCode())
                                    && websiteInfo.getStatusFlag().equals(0))
                            .sorted(Comparator.comparing(WebsiteConfig::getOrderNum))
                            .map(websiteInfo -> new WebsiteAllVo().setTitle(websiteInfo.getName())
                                    .setPath(websiteInfo.getPath()))
                            .collect(Collectors.toList());
                    return websiteAllVo.setTitle(websiteConfig.getName()).setPath(websiteConfig.getPath()).setChildren(children);
                })
                .collect(Collectors.toList());
        return result;
    }

    /**
     * 获取网站网址列表
     *
     * @param dto
     */
    @Override
    public List<WebsiteListVo> list(WebsiteListDto dto) {

        List<WebsiteConfig> websiteConfigsAll = baseMapper.selectList(new QueryWrapper<>());

        // 获取网址类别
        return websiteConfigsAll.stream()
                .filter(websiteConfig -> websiteConfig.getType().equals(1))
                .sorted(Comparator.comparing(WebsiteConfig::getOrderNum))
                .map(websiteConfig -> {
                    WebsiteListVo websiteListVo = new WebsiteListVo();
                    BeanUtils.copyProperties(websiteConfig, websiteListVo);
                    List<WebsiteListVo> children = websiteConfigsAll.stream()
                            .filter(websiteInfo -> {
                                if (StringUtil.isNotBlank(dto.getName())) {
                                    return websiteInfo.getName().contains(dto.getName());
                                }
                                return true;
                            })
                            .filter(websiteInfo -> websiteConfig.getCode().equals(websiteInfo.getTypeCode()))
                            .map(websiteInfo -> {
                                WebsiteListVo websiteVo = new WebsiteListVo();
                                BeanUtils.copyProperties(websiteInfo, websiteVo);
                                websiteVo.setTypeName(websiteConfig.getName());
                                return websiteVo;
                            })
                            .collect(Collectors.toList());
                    websiteListVo.setChildren(children);
                    return websiteListVo;
                })
                /*.filter(websiteListVo -> CollectionUtils.isNotEmpty(websiteListVo.getChildren()))*/
                .collect(Collectors.toList());
    }

    /**
     * 获取网站类型列表
     */
    @Override
    public List<WebsiteTypesVo> types() {
        WebsiteConfig websiteConfigDo = new WebsiteConfig();
        websiteConfigDo.setType(1).setStatusFlag(0);
        QueryWrapper<WebsiteConfig> queryWrapper = new QueryWrapper<>(websiteConfigDo);
        List<WebsiteConfig> websiteConfigs = baseMapper.selectList(queryWrapper);
        return websiteConfigs.stream()
                .map(websiteConfig -> new WebsiteTypesVo(websiteConfig.getCode(), websiteConfig.getName()))
                .collect(Collectors.toList());
    }

    /**
     * 删除网站信息
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        WebsiteConfig websiteConfigPo = baseMapper.selectById(id);
        if (ObjectUtils.isNull(websiteConfigPo)) {
            throw new RRException("记录不存在！");
        }
        if (websiteConfigPo.getType() == 1) {
            WebsiteConfig websiteConfigDo = new WebsiteConfig();
            websiteConfigDo.setTypeCode(websiteConfigPo.getCode());
            QueryWrapper<WebsiteConfig> queryWrapper = new QueryWrapper<>(websiteConfigDo);
            List<WebsiteConfig> websiteConfigs = baseMapper.selectList(queryWrapper);
            if (CollectionUtils.isNotEmpty(websiteConfigs)) {
                throw new RRException("该类型下存在记录，无法删除");
            }
        }
        baseMapper.deleteById(id);
    }

    /**
     * 编辑网站信息
     *
     * @param dto
     */
    @Override
    public void edit(WebsiteEditDto dto) {
        WebsiteConfig websiteConfigPo = baseMapper.selectById(dto.getId());
        if (ObjectUtils.isNull(websiteConfigPo)) {
            throw new RRException("记录不存在！");
        }
        Long userId = ShiroUtils.getUserId();
        Date currentTime = new Date();
        WebsiteConfig websiteDo = new WebsiteConfig();
        BeanUtils.copyProperties(dto, websiteDo);
        websiteDo.setUpdateBy(userId).setUpdateTime(currentTime);
        baseMapper.updateById(websiteDo);
    }

    /**
     * 新增配置网站信息
     *
     * @param dto
     */
    @Override
    public void add(WebsiteEditDto dto) {
        Long userId = ShiroUtils.getUserId();
        Date currentTime = new Date();
        WebsiteConfig websiteDo = new WebsiteConfig();
        BeanUtils.copyProperties(dto, websiteDo);
        String code = UUID.randomUUID().toString();
        websiteDo.setCreateBy(userId).setCreateTime(currentTime).setCode(code);
        baseMapper.insert(websiteDo);
    }
}
